
#include "MainWindow.h"

MainWindow::MainWindow()
{
	initScene();
	BViewer* viewer = new BViewer();
	{
		_camera = new BCamera();
		_camera->setPosition(30, 30, 30);
		_camera->setCenter(0, 0, 0);
		_camera->setNear(5);
		_camera->setFar(100);

		BSphereHandler* handler = new BSphereHandler;

		viewer->setCamera(_camera);
		viewer->setScene(_scene);
		viewer->setHandler(handler);
		viewer->setBorder(10);
		viewer->setStyleSheet("background:33, 33, 33;");
		//viewer->setStyleSheet("background-image:data/image/Multimedia_Icons_009.png");
	}

	BCheckBox* orthoCheckBox = new BCheckBox("ortho");
	viewer->addChild(orthoCheckBox);
	orthoCheckBox->setRect(10, 10, 100, 25);
	orthoCheckBox->setStyleSheet("arrow-color:200, 200, 200;arrow-color-hover:white;color:red;");

	BViewer* mainViewer = new BViewer();
	{
		BCamera* mainCamera = new BCamera();
		mainCamera->setPosition(30, 30, 30);
		mainCamera->setCenter(0, 0, 0);
		mainCamera->setNear(0.1);
		mainCamera->setFar(500);

		MainHandler* handler = new MainHandler();
		handler->setTargetCamera(viewer->camera());

		mainViewer->setCamera(mainCamera);
		mainViewer->setScene(_scene);
		mainViewer->setHandler(handler);
		mainViewer->setBorder(10);
		mainViewer->setStyleSheet("background:33, 33, 33;");
		//mainViewer->setStyleSheet("background-image:data/image/Multimedia_Icons_006.png");
	}

	BHBoxLayout* layout = new BHBoxLayout(this);
	layout->addWidget(viewer);
	layout->addWidget(mainViewer);

	connect(orthoCheckBox, Signal_Checked, &MainWindow::slotOrthoChecked);
}
MainWindow::~MainWindow()
{

}

void MainWindow::initScene()
{
	_scene = new BScene;


	for (int r = 0; r < 10; r++)
	{
		for (int c = 0; c < 10; c++)
		{
			float x = -100 + c * 20;
			float y = -100 + r * 20;

			BColor color(rand() % 255, rand() % 255, rand() % 255);
			BCylinder* cylinder = new BCylinder(5, 15, 12);
			cylinder->setColor(color);

			BNode* node = new BNode();
			node->addShape(cylinder);
			node->setPosition(BVector(x, y, 0));
			_scene->inserts(node);
		}
	}

	BMaterial* material1 = new BMaterial(BColor(200, 200, 200, 200));
	material1->setSpecular(200, 120, 0);
	material1->setShininess(3);

	BSphere* sphere = new BSphere(10, 24, 12);
	sphere->setPosition(0, 0, 50);
	sphere->setMaterial(material1);
	sphere->setTransparent(true);
	_scene->inserts(sphere);

	BLight* light = new BLight();
	{
		light->setPosition(100, 100, 100);

		BSphere* sphere = new BSphere(1, 24, 12);
		sphere->setLighting(false);
		sphere->setColor(light->diffuse());
		light->addShape(sphere);
	}
	_scene->inserts(light);

}

void MainWindow::slotOrthoChecked(BObject* object, const BValue& value)
{
	bool checked = value;
	if (checked)
	{
		_camera->setOrtho(true);
		_camera->project(-80, 80, -50, 50, 5, 200);
	}
	else
	{
		_camera->setOrtho(false);
		_camera->project(36.0, 5, 200);
	}
}
